using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System.Text.RegularExpressions;
using UnityEngine.Windows;
public class Dialogue:SingletonPattonMonoAutoBase<Dialogue>
{
    private Transform DialoguePanelObj;
    private TextAsset textAsset;
    List<string> Dia = new List<string>();
    public int index = -1;
    private Tween tween;
    private void Awake()
    {
       
    }
    private void Start()
    {
        UIMethod.GetInstance().GetOrAddComponentInChild<Button>(DialoguePanelObj.gameObject, "Next").onClick.AddListener(ReturnText);
        DiaStart("Dia");
    }
    public void DiaStart(string name)
    {
        DialoguePanelObj = UIMethod.GetInstance().GetOrAddComponentInChild<Transform>(UIManager.GetInstance().CanvasObj, "Dialogue");
        Dia.Clear(); index = -1;
        textAsset = Resources.Load<TextAsset>(name);
        var diadate = textAsset.text.Split("\n");
        foreach (string item in diadate)
        {
            Dia.Add(item);
        }
      
        DialoguePanelObj.localPosition = Vector3.zero;
        ReturnText();
      
    }
    public void ReturnText()
    {
     
        if(tween!=null&&tween.IsActive())
        {
            tween.Complete();
            if (DialoguePanelObj.Find("Text").GetComponent<Text>().text!= Dia[index])
            return;
        }
        index += 1;
        if (index>= Dia.Count)
        {
            DialoguePanelObj.gameObject.SetActive(false);
              index = -1;
            return ;
        }
        switch (Dia[index].Trim())
        {
            case "HeadLeft":
                index += 1;
                ShowHeadLeft(Dia[index]);
                return;
            case "HeadRight":
                index += 1;
                ShowHeadRight(Dia[index]);
                return;
            case "A1":         
                    index += 1;
                    Show(Dia[index]);              
                return;
        }
       
    }
    private void ShowHeadLeft(string Spritename)
    {
        DialoguePanelObj.Find("Left").GetComponent<Image>().sprite = GameRoot.GetInstance().AssetsFactory.LoadSprite(Spritename);
        DialoguePanelObj.Find("Right").gameObject.SetActive(false);
        DialoguePanelObj.Find("Left").gameObject.SetActive(true);
        ReturnText();
    }
    private void ShowHeadRight(string Spritename)
    {
        DialoguePanelObj.Find("Right").GetComponent<Image>().sprite = GameRoot.GetInstance().AssetsFactory.LoadSprite(Spritename);
        DialoguePanelObj.Find("Right").gameObject.SetActive(true);
        DialoguePanelObj.Find("Left").gameObject.SetActive(false);
        ReturnText();
    }
    private void Show(string Message)
    {
        Camera.main.GetComponent<AudioSource>().clip = GameRoot.GetInstance().AssetsFactory.LoadAudioclip(Message.TrimEnd());
       
        Camera.main.GetComponent<AudioSource>().Play();
        int number=(Regex.Matches(Message, @"[\u4e00-\u9fa5]").Count)/4;
        number += 1;
        DialoguePanelObj.Find("Text").GetComponent<Text>().text = "";
        tween=DialoguePanelObj.Find("Text").GetComponent<Text>().DOText(Message, number);
    }
    private void Quite()
    {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
    }
}

